package com.example.system.controller;

import com.example.system.entity.User;
import com.example.system.service.SystemService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.misc.BASE64Decoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.StandardCharsets;


@Controller
@RequestMapping("/")
public class SystemController {

    final static Logger logger = LoggerFactory.getLogger(SystemController.class);

    @Resource(name = "SystemServiceImpl")
    SystemService systemService;

   // @RequestMapping(value = "/login",method = RequestMethod.POST)
    @RequestMapping("/login")
    public String login(HttpServletRequest request, @RequestParam("checkcode") String checkcode,
                        @ModelAttribute("user") User user) {
        //获取base64转码后的用户名、密码
        String userNameBase64 = user.getUser_name();
        String passwordBase64 = user.getPassword();
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] userNameByte ;
        byte[] passwordByte ;
        try {
            userNameByte = decoder.decodeBuffer(userNameBase64);
            passwordByte = decoder.decodeBuffer(passwordBase64);
            //获得解密后的用户名,密码
            String userName = new String(userNameByte, StandardCharsets.UTF_8);  // 比" UTF-8"  方式要快一些
            String password = new String(passwordByte, StandardCharsets.UTF_8);

            user.setUser_name(userName);
            user.setPassword(password);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String valcode = (String) request.getSession().getAttribute("imageCode");
        if (checkcode.equals(valcode)){
           User u =  systemService.queryUser(user);
           if (!StringUtils.isEmpty(u)){
               HttpSession session = request.getSession();
               session.setAttribute("user",u);
               logger.info("SessionID="+session.getId(),"User-->"+u.toString());
           }else{
               request.setAttribute("msg","用户名或密码不正确!");
               return "login";
           }
        }else{
            request.setAttribute("msg","验证码不正确!");
            return "login";
        }
       return "index";
    }


    @RequestMapping("/saveUser")
    public String saveUser(@ModelAttribute User user){

        systemService.saveUser(user);
        return "redirect:queryUser";
    }

    @RequestMapping("/queryUser")
    public String queryUser(@ModelAttribute User user,
           @RequestParam(value="pageNum",defaultValue="1")int pageNum,
           @RequestParam(value="pageSize",defaultValue="5")int pageSize,
           HttpServletRequest request){

        PageInfo<User> pageInfo =  systemService.queryInfo(user,pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "system/user_items";
    }


}
